<?php
/*print_r($_POST);
	exit(0);*/
	
if($_POST)
{
	
	$dirController 	= '../../controllers/';
	$dirView 		= '../../themes/'.$_POST['_theme'].'/';
	$dirModel		= '../../models/';
	
	// ========================== Generating Controller =========================
	$old = $dirController.'testController.php';
	$new = $dirController.$_POST['_filename'].'Controller.php';
	copy($old, $new) or die("Unable to copy $old to $new.");
	//read the entire string
	$str=implode("\n",file($new));
	$fp=fopen($new,'w');
	
	//replace something in the file string - this is a VERY simple example
	$str = str_replace('<Controller Information>',$_POST['_filename'].'Controller: '.$_POST['_info'], $str);
	$str = str_replace('Author:','Auther: '.$_POST['_author'],$str);
  	$str = str_replace('Date:','Date: '.date('d,M,Y'), $str);
	$str = str_replace('testController',$_POST['_filename'].'Controller', $str);
												  
	//now, TOTALLY rewrite the file
	fwrite($fp,$str,strlen($str));
	fix_file($new); //just upload the file
	// ========================= Controller writing ends ============================
	
	
	// ========================== Generating Model =========================
	$old = $dirModel.'testModel.php';
	$new = $dirModel.$_POST['_filename'].'Model.php';
	copy($old, $new) or die("Unable to copy $old to $new.");
	//read the entire string
	$str=implode("\n",file($new));
	$fp=fopen($new,'w');
	
	//replace something in the file string - this is a VERY simple example
	$str = str_replace('<Model Information>',$_POST['_filename'].'Montroller: '.$_POST['_info'], $str);
	$str = str_replace('Author:','Auther: '.$_POST['_author'],$str);
  	$str = str_replace('Date:','Date: '.date('d,M,Y'), $str);
	$str = str_replace('testModel',$_POST['_filename'].'Model', $str);
												  
	//now, TOTALLY rewrite the file
	fwrite($fp,$str,strlen($str));
	fix_file($new); //just upload the file
	// ========================= Model writing ends ============================
	
	
	// ========================== Generating View =========================
	/*$new = $dirView.$_POST['_filename'].'.php';
	$fp=fopen($new,'w');
	
	$str  = "<!--";
	$str .= "\r".$_POST['_filename'].'.php';
	$str .= "\rCreated by ".$_POST['_author']." on ".date('d,M,Y');
	$str .= "\r-->";
												  
	//now, TOTALLY rewrite the file
	fwrite($fp,$str,strlen($str));
	fix_file($new);*/ //just upload the file
	// ========================= View writing ends ============================
	
	
	
	/*print_r($_POST);
	exit(0);*/
	
	header('Location: index.php?cmd=created');
}

//----------------------------------------------------------------------//
 
function fix_file( $file_path ) {
    $handle = @fopen($file_path, "r"); // Open file form read.
 
    if ($handle) {
        $i = 1;
        while (!feof($handle)) { // Loop til end of file.
            $buffer = fgets($handle); // Read a line.
            $trimmed_buffer = trim($buffer);
 
            if (! empty($trimmed_buffer)) // Check for string.
                $fixed_content .= rtrim($buffer)."\r\n";
            $i++;
        }
        fclose($handle); // Close the file.
    }
    else
        echo "No handle for reading";
 
    rewrite_file($file_path, $fixed_content);
 
}
 
function rewrite_file( $file_path, $fixed_content ) {
    if (is_writable($file_path)) {
        $handle2 = @fopen($file_path, "w+"); // Open file form read.
        if ($handle2) {
            if (!fwrite($handle2, $fixed_content))
                echo "Cannot write to file ($file_path)";
            else
                echo "";
            fclose($handle2);
        }
        else {
            echo "No handle for writing";
        }
    }
    else {
        echo "The file $file_path is not writable";
    }
}
?>